package com.sohoo.ftp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission;
import org.apache.ftpserver.usermanager.impl.WritePermission;

public class FtpDemo {
	public static void main(String[] args) throws Exception {
		FtpServerFactory factory = new FtpServerFactory();
		ListenerFactory listenerFactory = new ListenerFactory();
		listenerFactory.setPort(2121);
		Listener portListener = listenerFactory.createListener();
		Map<String, Ftplet> ftplets = factory.getFtplets();
		ftplets.put("test", new FtpLetDemo());
//		UserManager 
		UserManager userManager = factory.getUserManager();
		BaseUser user = new BaseUser();
		user.setName("test");
		user.setPassword("test");
		user.setHomeDirectory("D:/upload");
		//设置用户名 密码
		//添加权限
		List<Authority> authorities = new ArrayList<Authority>();
		authorities.add(new WritePermission());
		authorities.add(new  ConcurrentLoginPermission(10, 10));
		user.setAuthorities(authorities);
		userManager.save(user);
		System.out.println("Ftp服务器启动！");
//		String[] userNames = userManager.getAllUserNames();
//		System.out.println(Arrays.toString(userNames));
		factory.addListener("default", portListener);
		FtpServer ftpServer = factory.createServer();
		ftpServer.start();
	}
}
